package activity.personal.qianbao;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.util.Observable;
import java.util.Observer;

import base.BaseActivity;
import base.MyApplcation;
import presenter.SysTemPresenter;
import presenter.UserPresenter;
import util.utls.ByAlert;
import util.utls.Const;
import util.utls.IsUtilUid;
import util.utls.SPUtils;
import util.utls.TimeCount;
import util.volley.ResultEntity;

/**
 * 忘记原支付密码(1,获取短信验证码)
 */
public class ZhiFuMimaDuanXinaYanZhengActivity extends BaseActivity implements Observer {
    ImageView back;//返回图片
    TextView tvTitle, add_lift_tv;//标题
    TextView wangji_pass_phone;//手机号
    TextView wangji_pass_send;//发送验证码
    EditText wangji_pass_yzm;
    TextView pass_one_ok;//下一步
    UserPresenter userPresenter;
    String mobile;//手机号
    String NewsPwd;
    private TimeCount time;// 倒计时
    SysTemPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wang_ji_zhi_fu_pass);
        tvTitle.setText(R.string.zhifumimaduanxingyanzheng);
        pass_one_ok.setText("完成");
        userPresenter = new UserPresenter(this);
        presenter=new SysTemPresenter(this);
        mobile = (String) SPUtils.get(this, null, Const.MOBILE, "");
        NewsPwd=getIntent().getStringExtra("NewsPwd");

        if (!mobile.equals("")) {
            try {
                String hintphone = mobile.substring(0, 3) + "****"
                        + mobile.substring(7, 11);
                wangji_pass_phone.setText("请输入" + hintphone + "收到的短信校验码");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            wangji_pass_phone.setText("");
        }
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.wangji_pass_send://发送验证码
                userPresenter.sendVerificationCode("+86", mobile, 2 + "");
                time = new TimeCount(60000, 1000, wangji_pass_send);// 构造CountDownTimer对象
                time.start();
                break;
            case R.id.pass_one_ok:
                presenter.onSttringPayPwd(IsUtilUid.isUid(),NewsPwd);

                SPUtils.put(MyApplcation.ctx, null,
                        Const.ZHIFUMIMA, NewsPwd);
                break;

            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == UserPresenter.send_code_success) {
            }
            if (entity.getEventType() == UserPresenter.send_code_fail) {
            }
            if(entity.getEventType()==UserPresenter.yanzheng_code_success){
                ByAlert.alert(entity.getData());
                finish();
            }
            if(entity.getEventType()==UserPresenter.yanzheng_code_fail){
                ByAlert.alert(entity.getData());
            }if (entity.getEventType() == presenter.SttringPay_success) {
                SPUtils.put(this, null,Const.PaySecret, NewsPwd+"000");
                ByAlert.alert(entity.getData()+"");
                finish();
                Intent intent=new Intent("FINISHSEND");
                sendBroadcast(intent);
            }
        }
    }
}
